package cn.yitun.chat.yitunserverimcore.general.common.service.room.impl;

import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.base.chat.ChatItem;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.room.RoomChatContentDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.room.RoomChatItemDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.room.RoomChatContentInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.room.RoomChatContentVo;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimbase.dao.room.RoomChatContentDao;
import cn.yitun.chat.yitunserverimbase.dao.room.RoomChatItemDao;
import cn.yitun.chat.yitunserverimcore.general.common.service.room.RoomChatContentService;
import cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager.ChatManager;
import cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager.LastChatManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.chat.RoomChatPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yitun
 * @date: 2016年8月16日 上午9:38:51
 */
@Service
public class RoomChatContentServiceImpl
		extends CrudServiceImpl<RoomChatContentDao,RoomChatContentDto, RoomChatContentInfo, RoomChatContentVo>
		implements RoomChatContentService {

	@Resource
	private ChatManager chatManager;
	@Resource
	private RoomChatItemDao roomChatItemDao;
	@Resource
	private RoomChatPush roomChatPush;
	@Resource
	private LastChatManager lastChatManager;



	/**
	 * 记录聊天室的聊天内容
	 * 
	 * @author: yitun
	 * @createDate: 2016年8月19日 下午3:26:10
	 * @update: yitun
	 * @updateDate: 2016年8月19日 下午3:26:10
	 */
	@Transactional
	public void addRoomChatLog(UserDto userData, Long messageId, Long roomId, List<ChatItem> chatItemList, long timestamp) {
		messageId = (null == messageId || "".equals(messageId)) ? IdGen.getNumber() : messageId;
		if (null != userData) {
			RoomChatContentDto content = createRoomChatContent(messageId, roomId, userData, timestamp);
			dao.save(content);
			List<RoomChatItemDto> list = new ArrayList<>();
			for (ChatItem item : chatItemList) {
				RoomChatItemDto chatLog = new RoomChatItemDto();
				chatLog.setMessageId(messageId);
				chatLog.setRoomChatContentId(content.getId());
				chatLog.setRoomId(content.getRoomId());
				chatLog.setUserId(content.getUserId());
				chatLog.setRank(item.getRank());
				chatLog.setSection(item.getSection());
				chatLog.setType(item.getType());
				chatLog.setValue(item.getValue());
				chatLog.setFilterValue(item.getFilterValue());
				list.add(chatLog);
			}
			roomChatItemDao.saveList(list);
		}
	}

	private RoomChatContentDto createRoomChatContent(Long messageId, Long roomId, UserDto userData, long timestamp) {
		RoomChatContentDto chatLog = new RoomChatContentDto();
		chatLog.setMessageId(messageId);
		chatLog.setRoomId(roomId);
		chatLog.setRoomName("");
		chatLog.setUserHead(userData.getHead());
		chatLog.setUserId(userData.getId());
		chatLog.setUserName(userData.getName());
		chatLog.setUserNickname(userData.getNickname());
		chatLog.setIsSend("1");
		return chatLog;
	}

}
